package com.ler.yrmall.config;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author leron
 */
@Configuration
public class LongToStringSerialize extends JsonSerializer<List<Long>> {

    @Override
    public void serialize(List<Long> value, JsonGenerator gen, SerializerProvider serializerProvider) throws IOException {
        if (value != null) {
            List<String> collect = value.stream().map(String::valueOf).collect(Collectors.toList());
            String[] array = collect.toArray(new String[]{});
            gen.writeArray(array,0,collect.size());
        }
    }
}